package com.meevle.web;

import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import org.springframework.beans.factory.annotation.Required;

public class SimpleNavigationFactory implements INavigationFactory{

	private Map<Object, Object> pages;
	private String cssClassnameCurrentPage;
	private String servletSuffix;
	
	public SimpleNavigationFactory()
	{}
	
	public Map<Object, Object> getPages() {
		return pages;
	}
	
	@Required
	public void setPages(Map<Object, Object> pages) {
		this.pages = pages;
	}
	
	public String getCssClassnameCurrentPage() {
		return cssClassnameCurrentPage;
	}
	
	@Required
	public void setCssClassnameCurrentPage(String cssClassnameCurrentPage) {
		this.cssClassnameCurrentPage = cssClassnameCurrentPage;
	}
	
	public String getServletSuffix() {
		return servletSuffix;
	}
	
	@Required
	public void setServletSuffix(String servletSuffix) {
		this.servletSuffix = (servletSuffix==null || servletSuffix.trim().equals("")?"":servletSuffix);
	}
	
	public String getNavigation(Object servlet)
	{
		StringBuffer buffer = new StringBuffer();
		buffer.append("<ul>");

		Iterator<Entry<Object, Object>> it = pages.entrySet().iterator();
		while(it.hasNext())
		{
			Entry<Object, Object> pairs = it.next();
			if(servlet.getClass().getSimpleName().equals(pairs.getKey()) )
				buffer.append("\n<li class=\""+cssClassnameCurrentPage+"\"><a href=\""+pairs.getKey()+servletSuffix+"\">"+pairs.getValue()+"</a></li>");
			else
				buffer.append("\n<li><a href=\""+pairs.getKey()+ servletSuffix +"\">"+pairs.getValue()+"</a></li>");
		}
		buffer.append("\n</ul>");
		return buffer.toString();
	}
}
